package test_achieve.courierapp;

import constant.Constant;
import constant.Pm;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import test_achieve.LogRead;
import util.tools.BusinessTools;
import util.tools.CommonTools;
import cucumber.api.java.en.Given;
import io.appium.java_client.TouchAction;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import util.JdbcUtil;

import java.math.BigDecimal;
import java.util.List;


/**
 * Created by chenkuo on 2017/6/28.
 */

public class GrabOrder {

    @Given("^退出当前已登录的闪送员$")
    public void 退出当前已登录的闪送员() throws Throwable {
        if (Constant.driverCourier != null) {
            logOut();
        }
    }

    @Given("^登录闪送员打开听单并更新位置：(.*)$")
    public void 登录闪送员打开听单并更新位置(String number) throws Throwable {
        loginCouier(number);
        Oneself oneself = new Oneself();
        oneself.进入闪送员主页面();
        oneself.openListening();
        updateLocation();
    }

    @Given("^登录闪送员APP账户:(.+)$")
    public void loginCouier(String number) throws Throwable {
        if (Constant.driverCourier ==null){
            System.out.println("Constant.driverCourier ==null");
        }

        System.out.println("开始执行打开APP");
        if (Constant.driverCourier != null) {//判断是否第一次登录
            System.out.println("这不是第一次登录");
            try {
                BusinessTools.checkCourierAccount(number);//保持为需要账户
            } catch (Exception e) {
                BusinessTools.quitCourierDriver();
                BusinessTools.startCourierApp(number);//失败重新启动
            }

        } else {
            System.out.println("这是第一次登录");
            BusinessTools.startCourierApp(number);//启动app
        }
    }

    @Given("^登录闪送员APP账户" + Pm.p2)
    public void loginCouier(String number, String uuid) throws Throwable {
        BusinessTools.startCourierApp(number, "127.0.0.1:4723", uuid);
    }

    @Given("^更新了当前地理位置$")
    public void updateLocation() throws Throwable {

        TouchAction action = new TouchAction(Constant.driverCourier);
        Constant.driverCourier.findElement(By.id("local_btn")).click();//点击更新地址位置
        Thread.sleep(2000);
        action.tap(250, 250).perform();//点击空白位置防止一直加载
        CommonTools.waitElementLoad(Constant.driverCourier, "id", "custom_title_btn_left");//等待返回按钮出现
        Constant.driverCourier.findElement(By.id("custom_title_btn_left")).click();//点击返回按钮
    }


    @Given("^弹出抢单页面：(.*)$")
    public void 弹出抢单页面(String user) throws Throwable {
        try {
            BusinessTools.waitGrabButton(Constant.driverCourier, "rl_action_btn");//等待抢单按钮出现
        } catch (Exception e) {
            BusinessTools.pushExcludeNetworkImpact(user);
        }
    }

    @Given("^不会弹出抢单页面$")
    public void 不会弹出抢单页面() throws Throwable {
        Thread.sleep(6000);
        boolean btn1 = CommonTools.checkElementExit(Constant.driverCourier, "id", "rl_action_btn");
        Assert.assertEquals(false, btn1);
    }


    @Given("^点击抢单按钮$")
    public void clickGrabButton() throws Throwable {
        if (Constant.interfaceGrabStatus) {
            System.out.println("接口抢单，不需要点击抢单按钮");
        } else {
            BusinessTools.waitGrabButton(Constant.driverCourier, "rl_action_btn");
            Thread.sleep(8000);//等待8秒的阅读时间
            Constant.driverCourier.findElement(By.id("grab_task")).click();//点击抢单按钮
            CommonTools.waitElementLoad(Constant.driverCourier, "id", "title_txt");
        }
    }

    @Given("^抢单成功$")
    public void grabSuccess() throws Throwable {
//        Thread.sleep(12000);
    }

    @Given("^订单详情页面，点击地址进入进度详情$")
    public void 订单详情页面点击地址进入进度详情() throws Throwable {
        Constant.driverCourier.findElement(By.id("price_head")).click();//2级页面，点击公里数进入致电寄放页面
    }

    @Given("^点击致电寄方$")
    public void 点击致电寄方() throws Throwable {
        Constant.driverCourier.findElement(By.id("img_action")).click();//点击致电寄方
    }

    @Given("^致电寄方后完成就位$")
    public void 致电寄方后完成就位() throws Throwable {
        boolean btn = CommonTools.checkElementExit(Constant.driverCourier, "id", "ok_btn");//检查通话提醒是否存在
        if (btn) {
            Constant.driverCourier.findElement(By.id("ok_btn")).click();//确定通话提醒
            Thread.sleep(6000);//等待4秒倒计时
            Constant.driverCourier.findElement(By.id("b_know")).click();//点击“致电寄放”话术

            if (CommonTools.checkElementExit(Constant.driverCourier, "id", "android:id/button1")) {
                //如果是模拟器执行下面这个代码
                Constant.driverCourier.findElement(By.id("android:id/button1")).click();
            }
            Constant.driverCourier.findElement(By.id("img_action")).click();//点击“我已就位”
            Thread.sleep(1000);
            Constant.driverCourier.findElement(By.id("cancelbut")).click();//确认就位警告
            Thread.sleep(6000);//等待4秒倒计时
            Constant.driverCourier.findElement(By.name("我知道了")).click();//点击上门取件标准话术要求

        } else {
            Thread.sleep(1000);
            if (CommonTools.checkElementExit(Constant.driverCourier, "id", "android:id/button1")) {
                //如果是模拟器执行下面这个代码，如果是真机就注释掉不要执行
                Constant.driverCourier.findElement(By.id("android:id/button1")).click();
            }
            Constant.driverCourier.findElement(By.id("img_action")).click();//点击我已就位
            Thread.sleep(1000);
        }
    }


    @Given("^闪送员已就位$")
    public void inPlace() throws Throwable {
        //抢单后自动跳转至订单详情中
        订单详情页面点击地址进入进度详情();
        点击致电寄方();
        致电寄方后完成就位();

    }

    @Given("^闪送员完成取件$")
    public void completePickup() throws Throwable {
        boolean btn = CommonTools.checkElementExit(Constant.driverCourier, "id", "b_know");//是否弹出“上门取件话术提醒”
        if (btn) {
            Thread.sleep(1000);
            Constant.driverCourier.findElement(By.id("b_konw")).click();//确定上门取件话术要求
        }
        Constant.driverCourier.findElement(By.id("img_action")).click();//点击验证取件密码
        pickupPhoto();
        Thread.sleep(2000);
        输入取件密码();
        CommonTools.waitElementLoad(Constant.driverCourier, "id", "cancelbut");
        Thread.sleep(2000);
        Constant.driverCourier.findElement(By.name("确认")).click();//确认取件用时
    }

    @Given("^取件拍照$")
    public void pickupPhoto() throws Throwable {
        if (CommonTools.checkElementExit(Constant.driverCourier, "name", "跳过")) {
            Constant.driverCourier.findElement(By.name("跳过")).click();
        } else {
            //进行拍照上传
        }
    }


    @Given("^输入取件密码$")
    public void 输入取件密码() throws Throwable {
        Constant.PickupPassword = String.valueOf(JdbcUtil.sqlInt("SELECT pickup_password FROM order2.infos WHERE order_number='" + Constant.OrderNumbuter.get(Constant.OrderNumbuter.size() - 1) + "'"));//获取订单取件密码
        WebElement passwordBox = Constant.driverCourier.findElement(By.id("et_pwd"));//输入取件密码
        passwordBox.sendKeys(Constant.PickupPassword);
        Thread.sleep(1000);

    }


    @Given("^闪送员完成收件：(.+)$")
    public void completeDelivery(int index) throws Throwable {

        Constant.driverCourier.findElement(By.id("img_action")).click();//点击致电收方按钮
        Thread.sleep(2000);
        boolean btn = CommonTools.checkElementExit(Constant.driverCourier, "id", "ok_btn");//送件通话地址确认提醒
        if (btn) {
            Constant.driverCourier.findElement(By.id("ok_btn")).click();//送件通话地址确认提醒

            CommonTools.waitElementLoad(Constant.driverCourier, "id", "b_know");//等待致电收方按钮弹出
            Thread.sleep(5000);
            Constant.driverCourier.findElement(By.id("b_know")).click();//点击致电收方
            Constant.driverCourier.findElement(By.id("b_know")).click();//点击上门述话要求
        }
        Thread.sleep(1000);
        点击收件按钮();
        Thread.sleep(1000);
        输入收件密码(index);
    }

    @Given("^点击收件按钮$")
    public void 点击收件按钮() throws Throwable {
        if (CommonTools.checkElementExit(Constant.driverCourier, "id", "android:id/button1")) {
            //如果是模拟器执行下面这个代码
            Constant.driverCourier.findElement(By.id("android:id/button1")).click();
        }
        Constant.driverCourier.findElement(By.id("img_action")).click();//点击收件按钮
    }


    @Given("^闪送员完成评价$")
    public void completeEvaluation() throws Throwable {
        点击完成();
        Thread.sleep(1000);
        评价用户();
        Thread.sleep(1000);
        Constant.driverCourier.findElement(By.id("btn_submit")).click();//提交评价
        CommonTools.waitElementLoad(Constant.driverCourier, "id", "show_map");
    }

    @Given("^评价用户$")
    public void 评价用户() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverCourier, "id", "com.ishansong:id/ratingStar");
        Point start = Constant.driverCourier.findElement(By.id("com.ishansong:id/ratingStar")).getLocation();
        //获得控件开始位置的坐标轴
        int startX = start.x;
        int startY = start.y;
//        System.out.println("startX"+startX);
//        System.out.println("startY"+startY);
        //获得控件坐标轴差
        Dimension q = Constant.driverCourier.findElement(By.id("com.ishansong:id/ratingStar")).getSize();
        int width = q.getWidth();
        int height = q.getHeight();
        //五星坐标位置
        int fiveX = startX + (width / 5 * 4);
        int fiveY = startY + (height / 2);
        TouchAction action = new TouchAction(Constant.driverCourier);
        action.tap(fiveX, fiveY).perform();//点击空白位置防止一直加载

    }


    @Given("^退出登录$")
    public void logOut() throws Throwable {
        Oneself oneself = new Oneself();
        oneself.进入我的闪送页面();
        CommonTools.swipeUp(Constant.driverCourier);
        CommonTools.swipeUp(Constant.driverCourier);
        Constant.driverCourier.findElement(By.id("unlogin_btn")).click();//点击退出

    }

    @Given("^获得闪送员现金账户余额$")
    public void getAccountBalance() throws Throwable {
        Oneself my = new Oneself();
        my.进入我的闪送页面();
        Thread.sleep(1000);
        String money = Constant.driverCourier.findElement(By.id("total_money")).getText();
        Constant.courierBalance = Float.parseFloat(money.substring(0, money.indexOf("元")));
        System.out.println("闪送员初始余额：" + Constant.courierBalance);
    }


    @Given("^确认收支明细订单总金额$")
    public void confirmRealIncome() throws Throwable {
        List<WebElement> tot = Constant.driverCourier.findElements(By.id("com.ishansong:id/amount"));
        String Amount = tot.get(2).getText();
        System.out.println("闪送员订单总收入：" + Amount);
        String courierTotalIncome = String.valueOf((int) Constant.courierTotalIncome);
        Assert.assertEquals(courierTotalIncome, Amount);
    }

    @Given("^确认闪送员现金账户余额$")
    public void confirmCashAccountBalance() throws Throwable {
        BigDecimal CouBalance = new BigDecimal(Constant.courierBalance);//闪送员之前现金账户
        BigDecimal CouBalanceChange = BusinessTools.courierMoneyCheck();//闪送员现金账户变动
        System.out.println("闪送员现金账户变动:" + CouBalanceChange);
        float courierBalance = CouBalanceChange.add(CouBalance).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();//闪送员理论余额=闪送员现金账户变动+闪送员之前现金账户
        Oneself my = new Oneself();
        my.进入我的闪送页面();
        Constant.driverCourier.findElement(By.id("menu_left")).click();
        Constant.driverCourier.findElement(By.id("menu_right")).click();//刷新余额
        Constant.driverCourier.findElement(By.id("total_money")).click();
        Thread.sleep(2000);
        float courierBalanceture = Float.parseFloat(Constant.driverCourier.findElement(By.id("tv_account_cash")).getText());//完成后实际现金余额
        System.out.println("完成后实际现金余额：" + courierBalanceture);
        Assert.assertEquals(courierBalance, courierBalanceture, 0.000000000f);
    }

    @Given("^判断抢单页面为预约单$")
    public void 判断抢单页面为预约单() throws Throwable {
        if (Constant.interfaceGrabStatus) {
            System.out.println("接口抢单，不需要判断抢单页面为预约单");
        } else {
            WebElement appointment = Constant.driverCourier.findElement(By.id("price_head"));//价格中包含预约
            boolean btn = appointment.getText().contains("预约");
            Assert.assertEquals(btn, true);
        }
    }

    @Given("^判断订单详情是否为预约单$")
    public void 判断订单详情是否为预约单() throws Throwable {
        if (Constant.interfaceGrabStatus) {
            System.out.println("接口抢单，不需要判断订单详情是否为预约单");
        } else {
            WebElement appointment = Constant.driverCourier.findElement(By.id("tv_task_warn"));//订单详情，预约单提示
            String text = appointment.getText().substring(0, 2);
            Assert.assertEquals(text, "预约");
        }

    }

    @Given("^判断订单详情是否为一取多投单$")
    public void 判断订单详情是否为一取多投单() throws Throwable {
        List<WebElement> list = Constant.driverCourier.findElements(By.id("from_addr_icon"));//寄件按钮
        int size = list.size();
        Assert.assertEquals(size, 2);
    }

    @Given("^今日订单中进入订单详情中$")
    public void 今日订单中进入订单详情中() throws Throwable {
        WebElement order = Constant.driverCourier.findElement(By.id("tasknum_txt"));
        order.click();
    }

    @Given("^点击完成$")
    public void 点击完成() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverCourier, "id", "rl_action_btn");//等待完成按钮出现
        Thread.sleep(2000);
        Constant.driverCourier.findElement(By.id("rl_action_btn")).click();//点击完成按钮
    }

    @Given("^订单二级目录进入收件2详情$")
    public void 订单二级目录进入收件2详情() throws Throwable {
        List<WebElement> btnList = Constant.driverCourier.findElementsByClassName("android.widget.TextView");//获取页面2个公里数
        WebElement btn = btnList.get(12);
        btn.click();
        System.out.println("点击了收件2详情");
    }

    @Given("^切换到我的订单中$")
    public void 切换到我的订单中() throws Throwable {
//        Constant.driverCourier.startActivity("com.ishansong", ".activity.MyOrderListActivity");
        Constant.driverCourier.pressKeyCode(4);
    }

    @Given("^确认订单状态：(.+)$")
    public void 确认订单状态订单状态(String status) throws Throwable {
        List<WebElement> orderStatus = Constant.driverCourier.findElements(By.id("status_txt"));
        String orderstatus = orderStatus.get(0).getText();
        Assert.assertEquals(orderstatus, status);
    }

    @Given("^抢单页面检查寄付标识$")
    public void 抢单页面检查寄付标识() throws Throwable {

    }

    @Given("^此订单没有已支付标识$")
    public void 此订单没有已支付标识() throws Throwable {

    }

    @Given("^点击验证取件密码$")
    public void 点击验证取件密码() throws Throwable {
        Constant.driverCourier.findElement(By.id("img_action")).click();//点击验证取件密码
        Thread.sleep(1000);
    }

    @Given("^物品确认$")
    public void 物品确认() throws Throwable {
        Constant.driverCourier.findElement(By.id("b_submit")).click();//物品确认确定按钮
    }

    @Given("^显示应收元：(.+)$")
    public void 显示应收应收元(float mon) throws Throwable {
        String moneySter = Constant.driverCourier.findElement(By.id("incoming_txt")).getText();
        String money = CommonTools.cutMiddle(moneySter, "收取", "元");
        BigDecimal moneybig = new BigDecimal(money);
        BigDecimal FarePremium = new BigDecimal(Constant.orderFarePremium);
        float moneyture = moneybig.subtract(FarePremium).floatValue();
        Assert.assertEquals(moneyture, mon, 0.000001);
    }


    @Given("^确认取件用时$")
    public void 确认取件用时() throws Throwable {
        Constant.driverCourier.findElement(By.id("cancelbut")).click();//确认取件用时
    }

    @Given("^确认寄付后闪送员现金账户余额$")
    public void 确认寄付后闪送员现金账户余额() throws Throwable {
    }


    @Given("^输入收件密码：(.+)$")
    public void 输入收件密码(int index) throws Throwable {
        List<Integer> list = JdbcUtil.sqlIntList("SELECT delivery_password FROM order2.infos WHERE order_number='" + Constant.OrderNumbuter.get(Constant.OrderNumbuter.size() - 1) + "'");
        if (!(list != null && list.size() > 0 && index < list.size())) {
            throw new RuntimeException("闪送员完成收件 接口有问题，list：" + list + " ,index:" + index);
        }
        int deliveryPassword = list.get(index);
        Thread.sleep(1000);
        WebElement DeliveryPassword = Constant.driverCourier.findElement(By.id("et_pwd"));
        DeliveryPassword.sendKeys(String.valueOf(deliveryPassword));//输入收件密码
    }


    @Given("^致电收方$")
    public void 致电收方() throws Throwable {
        Constant.driverCourier.findElement(By.id("img_action")).click();//点击致电收方按钮
    }

    @Given("^确认收支明细寄付订单总金额$")
    public void 确认收支明细寄付订单总金额() throws Throwable {
        List<WebElement> tot = Constant.driverCourier.findElements(By.id("com.ishansong:id/amount"));
        String Amount = tot.get(1).getText();
        System.out.println("闪送员订单总收入：" + Amount);
        Assert.assertEquals(String.valueOf(Constant.orderAmount), Amount);
    }

    @Given("^今日订单点击代取件$")
    public void 今日订单点击代取件() throws Throwable {
        Constant.driverCourier.findElement(By.partialLinkText("代取件")).click();
    }

    @Given("^关闭抢单页面$")
    public void 关闭抢单页面() throws Throwable {
        if (Constant.interfaceGrabStatus = false) {
            Constant.driverCourier.findElement(By.id("close")).click();//抢单页面，点击右上“X”
        }

    }

    @Given("^关闭订单取消通知$")
    public void 关闭订单取消通知() throws Throwable {
        try {
            CommonTools.waitElementLoad(Constant.driverCourier, "id", "com.ishansong:id/btnOk");
            Constant.driverCourier.findElement(By.id("com.ishansong:id/btnOk")).click();
        } catch (Exception e) {

        }

    }


}




